<template>
  <div class="app-container">
    <!-- 操作 -->
    <el-row :gutter="20">
      <el-col :span="9">
        <el-input v-model="query.title" placeholder="请输入标题查找" @keyup.enter.native="searchData()" />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="searchData()">查询</el-button>
        <el-tooltip class="item" effect="dark" content="添加数据" placement="top-start">
          <el-button type="primary" @click="add()">添加</el-button>
        </el-tooltip>
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-table :data="tableData" style="width: 100%; margin: 20px 0" stripe border :default-sort="{ prop: 'createTime', order: 'descending' }">
      <el-table-column align="center" label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + addIndex }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="标题" prop="title"> </el-table-column>
      <el-table-column align="center" label="类型" prop="typeName" :filters="typeList" :filter-method="filterType" filter-placement="bottom-end"> </el-table-column>
      <el-table-column align="center" label="推荐" prop="recommend">
        <template slot-scope="scope">
          <el-tag :type="scope.row.recommend === true ? 'success' : 'primary'" disable-transitions>{{ scope.row.recommend === true ? '是' : '否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="published">
        <template slot-scope="scope">
          <el-tag :type="scope.row.published === true ? 'success' : 'primary'" disable-transitions>{{ scope.row.published === true ? '发布' : '未发布' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime" sortable> </el-table-column>
      <el-table-column align="center" label="更新时间" prop="updateTime" sortable> </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="edit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :total="total" :current-page.sync="query.current" :page-size="query.pageSize" :page-sizes="[10, 20, 30, 40]" background layout="total, sizes, prev, pager, next, jumper" style="text-align: center" />
  </div>
</template>

<script>
import productTypeApi from '@/api/type'
import blogApi from '@/api/blog'

export default {
  name: 'ArticleList',
  data() {
    return {
      //查询项
      query: {
        current: 1,
        pageSize: 10,
        title: ''
      },
      total: 0,

      typeList: [],
      // 表格
      tableData: [], // 数据
      addIndex: 1 // 序号
    }
  },
  created() {
    this.getType()
    this.list()
  },
  methods: {
    // 查询
    searchData() {
      this.query.current = 1
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 页码查询
    handleCurrentChange(val) {
      this.query.current = val
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 显示条数查询
    handleSizeChange(val) {
      this.query.pageSize = val
      this.query.current = 1
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 标签筛选
    filterType(value, row) {
      return row.typeId === value
    },

    // 获取分类
    getType() {
      productTypeApi.list().then(response => {
        if (response.code === 20000) {
          response.data.data.filter(item => {
            if (item.level === '1') {
              this.typeList.push({
                text: item.name,
                value: item.id
              })
            }
          })
        } else {
          this.$message.error(response.message)
        }
      })
    },

    add() {
      this.$router.push('/article/articleAdd')
    },

    // 列表
    list() {
      blogApi.list(this.query).then(response => {
        if (response.code === 20000) {
          let data = response.data.data
          this.tableData = data.records

          this.query.current = data.current
          this.query.pageSize = data.size
          this.total = data.total
        } else {
          this.$message.error(response.message)
        }
      })
    },

    // 删除
    handleDel(id) {
      this.$confirm('此操作将永久删除被选中的博客, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const form = new FormData()
        form.append('id', id)

        blogApi.delById(form).then(response => {
          if (response.code === 20000) {
            this.$message.success(response.message)
            //刷新页面
            this.list()
          } else {
            this.$message.error(response.message)
          }
        })
      })
    },

    // 编辑按钮
    edit(id) {
      this.$router.push(`/article/articleEdit/${id}`)
    }
  }
}
</script>
